package com.bluesky.jwf.example1.forms;

import com.bluesky.jwf.ComponentFactory;
import com.bluesky.jwf.component.Component;
import com.bluesky.jwf.component.HyperLinkButton;
import com.bluesky.jwf.component.dialog.MessageDialog;
import com.bluesky.jwf.component.event.AbstractEventHandler;
import com.bluesky.jwf.component.event.EventHandler;
import com.bluesky.jwf.component.tree.TreeViewItem;
import com.bluesky.jwf.component.window.Window;
import com.bluesky.jwf.example1.domain.Function;

public class FunctionTreeViewItem extends TreeViewItem {

	@Override
	public void init(String html) {

		super.init(html);

	}

	public void refresh() {

		setModel(model);

	}

	@Override
	public void setModel(Object model) {

		super.setModel(model);

		final Function f = (Function) model;

		HyperLinkButton link = (HyperLinkButton) ComponentFactory.getInstance()
				.createComponent(HyperLinkButton.class.getSimpleName());

		String text = f.getName();
		if (!f.isDisable())
			text = "<b>" + f.getName() + "</b>";

		link.setText(text);

		if (f.getChildren().isEmpty()) {
			if (!f.isDisable()) {
				link.setClickHandler(new AbstractEventHandler() {
					@Override
					public void handle(Component target, Object parameter ) {

						DemoWindow wnd = (DemoWindow) getAncesterByClass(DemoWindow.class);
						wnd.invokeFunction(f.getUrl());
					}
				});
			}
		}
		contentTag.removeAllChildren();
		contentTag.addChild(link);
	}
}
